﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TheStar.Lib
{
    public class TheStarAlgorithm : BranchAndBoundEst
    {
        #region fields
        private readonly Hashtable table; 
        #endregion

        #region constructors
        public TheStarAlgorithm(IProblemGraphEst problem)
            : base(problem)
        {
            this.table = new Hashtable();
        }
        #endregion

        #region methods
        protected override void Add(Path path)
        {
            object[] tuple = (object[])this.table[path.Last];
            if (tuple == null)
            {
                this.table.Add(path.Last, new object[] { path.Cost, path });
                base.Add(path);
            }
            else
            {
                double cost = (double)tuple[0];
                Path storedPath = (Path)tuple[1];

                if (cost > path.Cost)
                {
                    base.Remove(storedPath);
                    this.table.Remove(storedPath.Last);

                    this.Add(path);
                }
            }
        } 
        #endregion
    }
}
